<?php

    class GroupController extends Controller {
        public $metaDescription = null;
        public $metaKeywords = null;
        public $linkCanoncical = null;
        public $title = null;
        public $layout = 'column_main';        

        public function actionIndex(){
            $url = new Url();            

            if(isset($_SESSION["user_edu_net"])){ 
                $id_user = intval($_SESSION["user_edu_net"]["user_id"]);
                $group_id = intval($_GET["group_id"]);
                $data_user = User::getRowById($id_user); 
                $data_group = Group::getRowById($group_id);

                $post_data = PostGroup::getPostsByReceiverID($group_id);

                $post_reply = ReplyGroup::getReplysByReceiverID($group_id);

                /*echo "<pre>";
                print_r($post_data);
                echo "</pre>";     */


                $this->render("index",array("data_user"=>$data_user,"data_group"=>$data_group,"post_data"=>$post_data,"post_reply"=>$post_reply));
            }
            else{
                $this->redirect($url->createUrl("user/login")); 
            }
        }
        
        public function actionGrade(){
            $url = new Url();            

            if(isset($_SESSION["user_edu_net"])){ 
                $id_user = intval($_SESSION["user_edu_net"]["user_id"]);
                $group_id = intval($_GET["group_id"]);
                $assignment_id = intval($_GET["assignment_id"]);
                $data_user = User::getRowById($id_user); 
                $data_group = Group::getRowById($group_id);

                $assignment_data = Assignment::getRowById($assignment_id);

                //$exercise_data = Exercise::getRowByGroupId($group_id);
                $exercise_data = Exercise::getRowByAssignmentId($assignment_id);
                
                /*echo "<pre>";
                print_r($exercise_data);
                echo "</pre>";*/ 


                $this->render("grade",array("data_user"=>$data_user,"data_group"=>$data_group,"assignment_data"=>$assignment_data,"exercise_data"=>$exercise_data));
            }else{
                $this->redirect($url->createUrl("user/login")); 
            }
        }
        
        public function actionTurnIn(){
            $url = new Url();            

            if(isset($_SESSION["user_edu_net"])){ 
                $id_user = intval($_SESSION["user_edu_net"]["user_id"]);
                $group_id = intval($_GET["group_id"]);
                $assignment_id = intval($_GET["assignment_id"]);
                $data_user = User::getRowById($id_user); 
                $data_group = Group::getRowById($group_id);

                $assignment_data = Assignment::getRowById($assignment_id);

                /*echo "<pre>";
                print_r($assignment_data);
                echo "</pre>";     */


                $this->render("turn_in",array("data_user"=>$data_user,"data_group"=>$data_group,"assignment_data"=>$assignment_data));
            }else{
                $this->redirect($url->createUrl("user/login")); 
            }
        }

        public function actionAjaxCreateGroup(){

            $group_name = isset($_POST["group_name"])?mysql_escape_string($_POST["group_name"]):"";
            $grade_id = isset($_POST["grade_id"])?mysql_escape_string($_POST["grade_id"]):"";
            $grade_subject = isset($_POST["grade_subject"])?mysql_escape_string($_POST["grade_subject"]):"";
            $user_id = isset($_POST["user_id"])?mysql_escape_string($_POST["user_id"]):"";

            $array_input['group_code'] = $this->genRandomString();
            $array_input['group_name'] = $group_name;
            $array_input['group_grade_id'] = $grade_id;
            $array_input['groưp_teacher_id'] = $user_id;
            $array_input['group_sub_id'] = $grade_subject;
            $array_input['group_status'] = 1;
            $array_input['group_creat_date'] = time();

            $row = Group::insertObject($array_input);
            $array_input_2['group_id'] = $row;
            $array_input_2['user_id'] = $user_id;
            $array_input_2['member_type'] = 1;
            $array_input_2['member_status'] = 1;
            $array_input_2['member_create_date'] = time();
            $row = Member::insertObject($array_input_2);
            if($row){                  
                echo 1;
            }else{
                echo 0;
            }

        }    
        
        public function actionAjaxUpdateGroup(){

            $group_id = isset($_POST["group_id"])?mysql_escape_string($_POST["group_id"]):"";
            $group_name = isset($_POST["group_name"])?mysql_escape_string($_POST["group_name"]):"";
            $grade_id = isset($_POST["grade_id"])?mysql_escape_string($_POST["grade_id"]):"";
            $grade_subject = isset($_POST["grade_subject"])?mysql_escape_string($_POST["grade_subject"]):"";            
        
            $array_input['group_name'] = $group_name;
            $array_input['group_grade_id'] = $grade_id;            
            $array_input['group_sub_id'] = $grade_subject;
            $array_input['group_status'] = 1;            
            $row = Group::updateObject($array_input,"group_id",$group_id);            
            if($row){                  
                echo 1;
            }else{
                echo 0;
            }

        }    

        public function actionAjaxJoinGroup(){

            $user_id = isset($_POST["user_id"])?mysql_escape_string($_POST["user_id"]):"";
            $group_code = isset($_POST["group_code"])?mysql_escape_string($_POST["group_code"]):"";            

            $group = Group::getRowByGroupCode($group_code);
            if(!$group){
                echo 3;
            }else{            
                $array_input_2['group_id'] = $group['group_id'];
                $array_input_2['user_id'] = $user_id;
                $array_input_2['member_type'] = 0;
                $array_input_2['member_status'] = 1;
                $array_input_2['member_create_date'] = time();
                $row = Member::insertObject($array_input_2);
                if($row){                  
                    echo 1;
                }else{
                    echo 0;
                }
            }            


        }    

        function genRandomString() {
            $length = 6;
            $characters = "0123456789abcdefghijklmnopqrstuvwxyz";
            $string = "";    
            for ($p = 0; $p < $length; $p++) {
                $string .= $characters[mt_rand(0, strlen($characters))];
            }
            return $string;
        }        

        public function actionAjaxSendComment(){            

            $stt_content = isset($_GET["stt_content"])?mysql_escape_string($_GET["stt_content"]):"";            
            $group_id = isset($_GET["group_id"])?mysql_escape_string($_GET["group_id"]):"";
            $user_id = isset($_GET["user_id"])?mysql_escape_string($_GET["user_id"]):"";            

            /*$rowUser = User::checkUserByName($receiver_name);

            if(!$rowUser){
                echo -3;exit;
            }*/

            $array_input['pGroup_sender_id'] = $user_id;
            $array_input['pGroup_receiver_id'] = $group_id;
            $array_input['pGroup_content'] = $stt_content;            
            $array_input['pGroup_create_date'] = time();
            $last_id = PostGroup::insertObject($array_input);
            if($last_id !=0){            
                echo 1;
            }else{
                echo 0;
            }
        }        
        
        public function actionAjaxSendReply(){

            $post_id = isset($_GET["post_id"])?mysql_escape_string($_GET["post_id"]):"";            
            $reply_content = isset($_GET["reply_content"])?mysql_escape_string($_GET["reply_content"]):"";
            $user_id = isset($_GET["user_id"])?mysql_escape_string($_GET["user_id"]):"";            
            $group_id = isset($_GET["group_id"])?mysql_escape_string($_GET["group_id"]):"";            
                        
            /*$rowUser = User::checkUserByName($receiver_name);

            if(!$rowUser){
                echo -3;exit;
            }*/
            
            $array_input['rGroup_post_id'] = $post_id;
            $array_input['rGroup_sender'] = $user_id;
            $array_input['rGroup_receiver'] = $group_id;            
            $array_input['rGroup_content'] = $reply_content;            
            $array_input['rGroup_create_date'] = time();
            $last_id = ReplyGroup::insertObject($array_input);
            if($last_id !=0){            
                echo 1;
            }else{
                echo 0;
            }
        }
        
        public function actionAssignment(){
            $url = new Url();            

            if(isset($_SESSION["user_edu_net"])){ 
                $id_user = intval($_SESSION["user_edu_net"]["user_id"]);
                $group_id = intval($_GET["group_id"]);
                $data_user = User::getRowById($id_user); 
                $data_group = Group::getRowById($group_id);

                $post_data = Assignment::getAssignmentByGroupID($group_id);

                $post_reply = ReplyAssignment::getReplysByReceiverID($group_id);
                
                $data_exercise = Exercise::getRowByUserId($id_user);                
                
                /*echo "<pre>";
                print_r($data_exercise);
                echo "</pre>";*/
                
                $this->render("assignment",array("data_user"=>$data_user,"data_group"=>$data_group,"post_data"=>$post_data,"post_reply"=>$post_reply,"data_exercise"=>$data_exercise));
            }else{
                $this->redirect($url->createUrl("user/login")); 
            }
        }
        
        public function actionAjaxSendAssignment(){            

            $assignment_title = isset($_GET["assignment_title"])?mysql_escape_string($_GET["assignment_title"]):"";            
            $assignment_description = isset($_GET["assignment_description"])?mysql_escape_string($_GET["assignment_description"]):"";            
            $assignment_link = isset($_GET["assignment_link"])?mysql_escape_string($_GET["assignment_link"]):"";            
            $assignment_file_name = isset($_GET["assignment_file_name"])?mysql_escape_string($_GET["assignment_file_name"]):"";            
            $group_id = isset($_GET["group_id"])?mysql_escape_string($_GET["group_id"]):"";
            $user_id = isset($_GET["user_id"])?mysql_escape_string($_GET["user_id"]):"";                        

            $array_input['teacher_id'] = $user_id;
            $array_input['group_id'] = $group_id;
            $array_input['assignment_title'] = $assignment_title;            
            $array_input['assignment_description'] = $assignment_description;            
            $array_input['assignment_link'] = $assignment_link;                        
            $array_input['assignment_file_name'] = $assignment_file_name;                        
            $array_input['assignment_create_date'] = time();
            
            $last_id = Assignment::insertObject($array_input);
            if($last_id !=0){            
                echo 1;
            }else{
                echo 0;
            }
        }        

        public function actionAjaxSendAssignmentReply(){

            $assignment_id = isset($_GET["assignment_id"])?mysql_escape_string($_GET["assignment_id"]):"";            
            $reply_content = isset($_GET["reply_content"])?mysql_escape_string($_GET["reply_content"]):"";
            $user_id = isset($_GET["user_id"])?mysql_escape_string($_GET["user_id"]):"";            
            $group_id = isset($_GET["group_id"])?mysql_escape_string($_GET["group_id"]):"";                                    
            
            $array_input['rAssign_assignment_id'] = $assignment_id;
            $array_input['rAssign_sender'] = $user_id;
            $array_input['rAssign_receiver'] = $group_id;            
            $array_input['rAssign_content'] = $reply_content;            
            $array_input['rAssign_create_date'] = time();
            $last_id = ReplyAssignment::insertObject($array_input);
            if($last_id !=0){            
                echo 1;
            }else{
                echo 0;
            }
        }
        
        public function actionAjaxSendExercise(){            
            
            $exercise_description = isset($_GET["exercise_description"])?mysql_escape_string($_GET["exercise_description"]):"";            
            $exercise_link = isset($_GET["exercise_link"])?mysql_escape_string($_GET["exercise_link"]):"";            
            $exercise_file_name = isset($_GET["exercise_file_name"])?mysql_escape_string($_GET["exercise_file_name"]):"";            
            $assignment_id = isset($_GET["assignment_id"])?mysql_escape_string($_GET["assignment_id"]):"";
            $group_id = isset($_GET["group_id"])?mysql_escape_string($_GET["group_id"]):"";
            $user_id = isset($_GET["user_id"])?mysql_escape_string($_GET["user_id"]):"";                        

            $array_input['assignment_id'] = $assignment_id;
            $array_input['user_id'] = $user_id;
            $array_input['group_id'] = $group_id;
            $array_input['exercise_link'] = $exercise_link;            
            $array_input['exercise_file_name'] = $exercise_file_name;            
            $array_input['exercise_description'] = $exercise_description;                        
            $array_input['exercise_create_date'] = time();
            
            $last_id = Exercise::insertObject($array_input);
            
            if($last_id !=0){            
                echo 1;
            }else{
                echo 0;
            }
        }        
        
        public function actionAjaxExerciseMark(){            
            
            $exercise = isset($_GET["exercise"])?mysql_escape_string($_GET["exercise"]):"";            
            $mark = isset($_GET["mark"])?mysql_escape_string($_GET["mark"]):"";                        

            $array_input['exercise_grade'] = $mark;            
            
            $last_id = Exercise::updateObject($array_input,"exercise_id",$exercise);
            
            if($last_id !=0){            
                echo 1;
            }else{
                echo 0;
            }
        }        
        
    }

?>
